package com.xiyueyezi.xiyueyezi;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Outline;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.xiyueyezi.xiyueyezi.service.LoginService;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";

    private Button loginBtn;   // 登录按钮
    private EditText et_id;    // 账号
    private EditText et_pwd;   // 密码
    private Handler uiHandler = new Handler(Looper.getMainLooper());


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();
    }

    private void initView() {
        loginBtn = findViewById(R.id.btn_login);
        et_id = findViewById(R.id.et_id);
        et_pwd = findViewById(R.id.et_password);

        loginBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "登录操作");
                String id = null;
                String pwd = null;
                //检查输入
                if (et_id.getText().toString().equals("")) {
                    et_id.setError("账号不能为空");
                } else if (et_pwd.getText().toString().equals("")) {
                    et_pwd.setError("密码不能为空");
                } else {
                    id = et_id.getText().toString();
                    pwd = et_pwd.getText().toString();
                    login_start(id, pwd);
                }
            }
        });
    }

    private void login_start(String id, String pwd) {
        //显示加载动画
        //TODO:
        LoginService loginService = new LoginService();
        loginService.startLogin(id, pwd, new LoginService.LoginCallback() {

            @Override
            public void onSuccess(String result) {
                SharedPreferences sp = getSharedPreferences("xiyueyezi", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("id", id);
                editor.putBoolean("isLogin", true);
                editor.apply();
                uiHandler.post(() -> {
                    //TODO:隐藏加载动画
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                });
            }

            @Override
            public void onFailure(String error) {
                uiHandler.post(() -> {
                    //TODO:隐藏加载动画
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
                });
            }
        });

    }



}